import 'package:bloc/bloc.dart';
import 'package:built_collection/built_collection.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/learning_content/collection_list_item/collection_list_item.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:learning_content_admin_pannel.core/core/domain/utils/with_previous_stream.dart';

part 'bloc_state.dart';

class LearningContentBloc extends Cubit<LearningContentBlocState?> {
  LearningContentBloc() : super(null) {
    stream = super.stream.cast();
    streamWithPrevious = stream.withPrevious;
  }

  @override
  late final Stream<LearningContentBlocState> stream;

  late final Stream<(LearningContentBlocState, LearningContentBlocState?)> streamWithPrevious;

  void initialize({
    required BuiltMap<String, LexicalItem> lexicalItemMap,
    required BuiltList<CollectionListItem> collectionListItems,
  }) async {
    if (state != null) {
      return;
    }

    emit(LearningContentBlocState(
      lexicalItemMap: lexicalItemMap,
      collectionListItems: collectionListItems,
    ));
  }

  void updateWith({
    BuiltMap<String, LexicalItem> Function()? lexicalItemMap,
    BuiltList<CollectionListItem> Function()? collectionListItems,
  }) {
    if (state == null) {
      return;
    }

    emit(state!.copyWith(
      lexicalItemMap: lexicalItemMap,
      collectionListItems: collectionListItems,
    ));
  }
}
